package Dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import Entity.Course;
import Entity.Room_ced;
import Entity.Self_Booked_Page;

import mapper.Coursemapper;
import mapper.Rankmapper;
import mapper.Schedulemapper;
import mapper.Selectionmapper;




public class coursedao implements Coursemapper {
	
    	
    	private  static SqlSession session;
  
		private static Coursemapper cm;
    
    	
    	
    	public coursedao() {
    		String resource="mybatis-config.xml";
            InputStream inputStream;
            try {
                inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory  sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
                 session=sqlSessionFactory.openSession();
                cm = session.getMapper(Coursemapper.class);
            }catch (IOException e){
                e.printStackTrace();
            }

	       
	    }
    	public void close(){
    		this.session.close();
    	}
    	

		public boolean CreatCourse(Course course) {
			rankdao rm = null;
			try {
				rm = new rankdao();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int course_Id = rm.getNum("course_Id");
			course.setCourse_Id(String.valueOf(course_Id));
			System.out.println(String.valueOf(course_Id));
			boolean iuser = this.cm.CreatCourse(course);
			System.out.println(course);
			System.out.println(iuser);
			if(iuser ){
			
			session.commit();}
			else{
				session.rollback();
			}
			return iuser;
		}
		
		@Override
		//删除课程
		public void deleteCourse(String cid) {
			this.cm.deleteCourse(cid);
			Selectionmapper sm = null;
			try {
				sm = new selectiondao();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			sm.delSelection(cid);
			session.commit();
			
		}

		@Override
		//根据id返回所有课程
		public List<Course> getAllCourseById(Self_Booked_Page sbp) {
			
			
			List<Course> selectList = this.cm.getAllCourseById(sbp);
	
			return selectList;
		}


		@Override
		public Self_Booked_Page getPage(Self_Booked_Page sbp) {
			
			Coursemapper cm = null;
			cm = new coursedao();
			List<Course> sl = cm.getAllCourseById(sbp);
			Schedulemapper sm = null;
			try {
				sm = new scheduledao();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for(int i=0 ;i<sl.size();i++ ){
				Course cc=sl.get(i);
				if(sm.getAllSchedule(sl.get(i).getCourse_Id(),sbp.getDate())==null){
					sl.remove(cc);
				}
			}
			Self_Booked_Page  sbp1 = new Self_Booked_Page();
			sbp1.setSum(sl.size());
			sbp1.setPage_n(sbp.getPage_n());
			sbp1.setId(sbp.getId());
			sbp1.setDate(sbp.getDate());
			sbp1.setInfo_s(1);
			sbp1.setInfo_e(1000);
			
			if(1000<=sl.size())
				sbp1.setC_list(sl.subList(0, 1000));
	        else if(1>sl.size())
	        	sbp1.setC_list(null);	
	        else
	        	sbp1.setC_list(sl.subList(0, sl.size()));
			return sbp1;
		}


		
		//得到所有课程
		public List<Course> getAllCourse() {
		
			List<Course> selectList = this.cm.getAllCourse();

			return selectList;
		}

		@Override
		//课程搜索函数,返回所有课程
		public List<Course> searchAllCourses(int currPage) {
			
			Coursemapper cm = null;
			cm = new coursedao();
			List<Course> co = cm.getAllCourse();
			//	        从第几条数据开始
	        int firstIndex = (currPage - 1) * 10;
	        //	        到第几条数据结束
	        int lastIndex = currPage * 10;
	        if(lastIndex<=co.size())
	        	return co.subList(firstIndex, lastIndex);
	        else if(firstIndex>=co.size())
	        	return null;
	        else
	        	return co.subList(firstIndex, co.size());
			
		}

		@Override
		//后端调用，返回查询教师情况下分页所有课程
		public List<Course> searchCourses(int currPage, String search) {
	
			Coursemapper cm = null;
			cm = new coursedao();
			List<Course> co = cm.getAllCourseLikeId(search);
			//	        从第几条数据开始
	        int firstIndex = (currPage - 1) * 10;
	        //	        到第几条数据结束
	        int lastIndex = currPage * 10;
	        if(lastIndex<=co.size())
	        	return co.subList(firstIndex, lastIndex);
	        else if(firstIndex>=co.size())
	        	return null;
	        else
	        	return co.subList(firstIndex, co.size());
		}

		@Override
		//数据库调用函数，未分页
		public List<Course> getAllCourseLikeId(String search) {
		
		
			List<Course> selectList = this.cm.getAllCourseLikeId(search);
		
			return selectList;
		}

		@Override
		public Course getCourse(String cid) {
	
			
			Course selectList = this.cm.getCourse(cid);

			return selectList;
		}

		@Override
		public Course queryCourseInfo(String rid, Date date, int order) {
			
			Schedulemapper sm = null;
			try {
				sm = new scheduledao();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String s1 = sm.querySchedule1(rid, date, order);
			Coursemapper mapper = null;
			mapper = new coursedao();
			Course selectList = mapper.getCourse(s1);
			return selectList;
			
		}
		@Override
		public String CreatAll(Course course, Room_ced room_ced) {
			
			Coursemapper cm = null;
			cm = new coursedao();
			boolean i = cm.CreatCourse(course);
			Schedulemapper sm = null;
			try {
				sm = new scheduledao();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			boolean j = sm.CreatSchedule(room_ced);
			System.out.println("hhhhhh");
			Rankmapper rk =null;
			try {
				rk = new rankdao();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String cid = String.valueOf(rk.getNum("course_Id"));
			System.out.println(rk.getNum("course_Id"));
			rk.UpdateNum("course_Id");
			System.out.println(rk.getNum("course_Id"));
			session.commit();
			return cid;
		}
		
		public void updatestu(String cid, int num) {
			Selectionmapper sm = null;
			try {
				sm = new selectiondao();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			List<String> sl = sm.courseRelatedStudents(cid);
			num = sl.size();
			System.out.println(num);
			this.cm.updatestu(cid, num);
			session.commit();
		}

}
